package com.lc.recovery.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.request.VersionOfApi;
import com.lc.recovery.http.response.VersionBean;
import com.lc.recovery.manager.ActivityManager;
import com.lc.recovery.other.AppConfig;
import com.lc.recovery.other.DoubleClickHelper;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.ui.dialog.UpdateDialog;
import com.lc.recovery.ui.fragment.HomeBusinessFragment;
import com.lc.recovery.ui.fragment.HomeRecoveryFragment;
import com.lc.recovery.ui.fragment.HomeWarehouseFragment;
import com.lc.recovery.ui.fragment.MessageBusinessFragment;
import com.lc.recovery.ui.fragment.MessageRecoveryFragment;
import com.lc.recovery.ui.fragment.MessageWarehouseFragment;
import com.lc.recovery.ui.fragment.MineBusinessFragment;
import com.lc.recovery.ui.fragment.MineRecoveryFragment;
import com.lc.recovery.ui.fragment.MineWarehouseFragment;
import com.lc.recovery.util.SPUtil;
import com.lc.recovery.util.constant.EvenConstant;
import com.lc.recovery.util.eventbus.Event;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import butterknife.ButterKnife;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/10/18
 *    desc   : 首页界面
 */
public final class HomeActivity extends AppActivity implements RadioGroup.OnCheckedChangeListener {
           private RadioGroup rgHomePageBottom;
           private RadioButton radioButton1, radioButton2, radioButton3;
           private List<Fragment> mFragmentList = new ArrayList<>();;
    @Override
    protected int getLayoutId() {
        return R.layout.home_activity;
    }

    @Override
    protected void initView() {

    }



           @Override
           protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               // TODO: add setContentView(...) invocation
               ButterKnife.bind(this);
               radioButton1 = findViewById(R.id.rb_home);
               radioButton2 = findViewById(R.id.rb_message);
               radioButton3 = findViewById(R.id.rb_my);
               rgHomePageBottom = findViewById(R.id.rg_home_page_bottom);
               rgHomePageBottom.setOnCheckedChangeListener(this);

               if (SPUtil.get(this, IntentKey.TYPE,"").equals("1")||SPUtil.get(this, IntentKey.TYPE,"").equals("2")){
                   radioButton2.setText("消息");
                   mFragmentList.add(new HomeBusinessFragment());
                   mFragmentList.add(new MessageBusinessFragment());
                   mFragmentList.add(new MineBusinessFragment());
               }else if (SPUtil.get(this, IntentKey.TYPE,"").equals("3")){
                   radioButton2.setText("消息");
                   mFragmentList.add(new HomeRecoveryFragment());
                   mFragmentList.add(new MessageRecoveryFragment());
                   mFragmentList.add(new MineRecoveryFragment());
               }else if (SPUtil.get(this, IntentKey.TYPE,"").equals("4")){
                   radioButton2.setText("库房");
                   mFragmentList.add(new HomeWarehouseFragment());
                   mFragmentList.add(new MessageWarehouseFragment());
                   mFragmentList.add(new MineWarehouseFragment());
               }else {
                   radioButton2.setText("消息");
                   mFragmentList.add(new HomeBusinessFragment());
                   mFragmentList.add(new MessageBusinessFragment());
                   mFragmentList.add(new MineBusinessFragment());
               }
               radioButton1.setChecked(true);
               VersionOf();
           }

    private void VersionOf() {
        EasyHttp.get(this)
                .api(new VersionOfApi()).request(new HttpCallback<VersionBean>(this) {


            @Override
            public void onSucceed(VersionBean data) {
                if (data.getVersion()> AppConfig.getVersionCode()){


                    new UpdateDialog.Builder(HomeActivity.this)

                            .setForceUpdate(true)

                            .setDownloadUrl(data.getUrl())
                            .show();
                }
//
            }


        });
    }

    @Override
    protected void initData() {


        SPUtil.put(this,"agree","agree");
        SPUtil.put(this,"hello","hello");
    }



    @Override
    public void onBackPressed() {
        if (!DoubleClickHelper.isOnDoubleClick()) {
            toast(R.string.home_exit_hint);
            return;
        }

        // 移动到上一个任务栈，避免侧滑引起的不良反应
        moveTaskToBack(false);
        postDelayed(() -> {
            // 进行内存优化，销毁掉所有的界面
            ActivityManager.getInstance().finishAllActivities();
            // 销毁进程（注意：调用此 API 可能导致当前 Activity onDestroy 方法无法正常回调）
            // System.exit(0);
        }, 300);
    }

           @Override
           public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
               switch (checkedId) {
                   case R.id.rb_home:
                       switchContent(mFragmentList.get(0));
                       break;

                   case R.id.rb_message:
                       switchContent(mFragmentList.get(1));
                       break;
                   case R.id.rb_my:
                       switchContent(mFragmentList.get(2));
                       break;
               }
           }
           private Fragment mContent = new Fragment();

           private void switchContent(Fragment to) {
               Log.e("addFragment", "### " + to.getId() + "  " + to.isAdded() + "  " + to.isHidden());
               if (mContent != to) {
                   FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                   if (!to.isAdded()) {
                       transaction.hide(mContent).add(R.id.fg_main, to).commitAllowingStateLoss();
                   } else {
                       transaction.hide(mContent).show(to).commitAllowingStateLoss();
                   }
                   mContent = to;
               }
           }
           @Override
           protected boolean isRegisterEventBus() {
               return true;
           }

           @Override
           protected void receiveEvent(Event event) {
               super.receiveEvent(event);
               if (event.getCode() == EvenConstant.TYPE){
                 int  presentInt = (int) event.getData();
                 if (presentInt==3){
                     mFragmentList.clear();
                     mFragmentList.add(new HomeRecoveryFragment());
                     mFragmentList.add(new MessageRecoveryFragment());
                     mFragmentList.add(new MineRecoveryFragment());
                     radioButton2.setText("消息");
                 }else if (presentInt==1||presentInt==2){
                     mFragmentList.clear();
                     mFragmentList.add(new HomeBusinessFragment());
                     mFragmentList.add(new MessageBusinessFragment());
                     mFragmentList.add(new MineBusinessFragment());
                     radioButton2.setText("消息");
                 }else {
                     mFragmentList.clear();
                     mFragmentList.add(new HomeWarehouseFragment());
                     mFragmentList.add(new MessageWarehouseFragment());
                     mFragmentList.add(new MineWarehouseFragment());
                     radioButton2.setText("库房");
                 }
                   radioButton1.setChecked(true);
               }

           }
           @Override
           protected void onSaveInstanceState(@NonNull @NotNull Bundle outState) {
//        super.onSaveInstanceState(outState);
           }
           @Override
           protected void onDestroy() {
               super.onDestroy();

           }

       }